/** 
  ******************************************************************************
  * @file    main.c
  * @author  SJU
  * @version V1.0
  * @date    2025-08
  * @brief   测试 KEY1和KEY2按键中断，KEY1动作使LED3反转，KEY2动作使LED2熄灭，
 ******************************************************************************
  * @attention
  * 实验平台:SJU积木式单片机学习套件 
 ******************************************************************************
 **/

#include "stm32f10x.h"
#include "bsp_4led.h"
#include "bsp_exti.h"
#include "systick.h"
#include "bsp_key.h"


void LED_Task(void)
{   LED1_ON();	
		SysTick_Delay_Nms(600);
		LED1_OFF();  
		SysTick_Delay_Nms(400);
	  LED2_ON();	
		SysTick_Delay_Nms(600);
		LED2_OFF();  
		SysTick_Delay_Nms(400);
	  LED3_ON();	
		SysTick_Delay_Nms(600);
		LED3_OFF();  
		SysTick_Delay_Nms(400);
}


void key_led(void)
{
	if(Read_KEY1()==0) LED3_ON();
	else LED4_OFF();
}

uint8_t  key1_pressed_flag=0;
uint8_t  key2_pressed_flag=0;

void key_state_scan(void)
{
	static uint8_t old_key1=1,old_key2=1;
	uint8_t now_key1=1,now_key2=1;
	
	now_key1 =Read_KEY1();
	if(old_key1==1 && now_key1==0)
		key1_pressed_flag=1;
	old_key1=now_key1;
	
	now_key2 =Read_KEY2();
	if(old_key2==1 && now_key2==0)
		key2_pressed_flag=1;
	old_key2=now_key2;
		
	
}
int main(void)
{
	LED_GPIO_Config();
	KEY_GPIO_Config();
	
	SysTick_Delay_Nms(100);
	
	while(1)
	{
	SysTick_Delay_Nms(1);	
		key_state_scan();
		if(key1_pressed_flag)
		{
			key1_pressed_flag=0;
			LED4_ON();
		}
		if(key2_pressed_flag)
		{
			key2_pressed_flag=0;
			LED4_ON();
		}
	}
}


extern uint8_t SEG_Code[16];

uint8_t key1_pressed_fiag=0;
uint8_t key2_pressed_fiag=0;
void scan_key(void)
{
	static uint8_t old_key1=1,old_key2=1;
	uint8_t now_key1,now_key2;
	now_key1=Read_KEY1();
	if( old_key1==1 &&  now_key1==0)
	{
		key1_pressed_fiag=1;
	}
	old_key1=now_key1;
	
	now_key2 =Read_KEY2();
	if(old_key2==1 &&  now_key2==0)
	{
		key1_pressed_fiag=1;
	}
	old_key2=now_key2;
		
}



/*********************************************END OF FILE**********************/
